package com.wc.fortnight_blue_bridge.Q240323.B_djwcb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/23 19:37
 * @description https://www.lanqiao.cn/problems/17028/learning/?contest_id=177
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int t, x;

    public static void main(String[] args) {
        t = sc.nextInt();
        int[][] map = new int[10][];
        map[0] = new int[]{0};
        map[1] = new int[]{1};
        map[2] = new int[]{6, 2, 4, 8};
        map[3] = new int[]{1, 3, 9, 7};
        map[4] = new int[]{6, 4};
        map[5] = new int[]{5};
        map[6] = new int[]{6};
        map[7] = new int[]{1, 7, 9, 3};
        map[8] = new int[]{6, 8, 4, 2};
        map[9] = new int[]{1, 9};
        while (t-- > 0) {
            x = sc.nextInt() % 10;
//            BigInteger x = sc.nextBigInteger();
            BigInteger p = sc.nextBigInteger().mod(new BigInteger(String.valueOf(map[x].length)));
//            BigInteger p = sc.nextBigInteger();
//            out.println(x.modPow(p, new BigInteger("10")).toString());
            int idx = Integer.parseInt(p.toString());
            out.println(map[x][idx]);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    BigInteger nextBigInteger() {
        return new BigInteger(next());
    }

    BigDecimal nextBigDecimal() {
        return new BigDecimal(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}